package com.tuo51.foudation.utils;

import com.tuo51.evaluate.vo.evaluate.EvaluateV2ExtInfoVO;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

public class EvaluationReportUtils {

    private static List generateLines(String text, boolean ul){
        List result = new ArrayList();
        String[] lines = text.split("(\\n|\\r\\n)");

        for(String line : lines){
            line = line.trim();
            if(StringUtils.isBlank(line)){
                continue;
            }

            //分解空格和冒号
            Map lineMap = new HashMap();
            String[] comps = line.split("(\\:|：)");
            if(comps.length == 1){
                comps[0] = comps[0].trim();
                if (comps[0].length() < 12 && !ul){
                    lineMap.put("hint", comps[0]);
                    lineMap.put("text", "");
                }else{
                    lineMap.put("hint", ul?"●":"");
                    lineMap.put("text", comps[0]);
                }
            }else {
                if (comps[0].length() < 12) {
                    lineMap.put("hint", comps[0].trim() + "：");
                    lineMap.put("text", line.substring(comps[0].length() + 1));
                } else {
                    lineMap.put("text", line.trim());
                }
            }

            result.add(lineMap);

        }

        return result;
    }

    private static Map generateSectionInfo(String title, String text, boolean ul){

        Map result = new HashMap();

        result.put("ul", false);
        result.put("sectionTitle", title);
        result.put("lines", generateLines(text, ul));

        return result;
    }

    private static List<Map> generateSectionMapsFromItem(EvaluateV2ExtInfoVO item){

        List result = new ArrayList();

        if(StringUtils.isNotBlank(item.getDevelopmentStatus()))
            result.add(generateSectionInfo("发育里程碑", item.getDevelopmentStatus(), false));
        if(StringUtils.isNotBlank(item.getObservation()))
            result.add(generateSectionInfo("重点观察项", item.getObservation(), true));
        if(StringUtils.isNotBlank(item.getGuide()))
            result.add(generateSectionInfo("指导方案", item.getGuide(), false));
        if(StringUtils.isNotBlank(item.getBringUp()))
            result.add(generateSectionInfo("能力拓展", item.getBringUp(), false));
        if(StringUtils.isNotBlank(item.getParentManual()))
            result.add(generateSectionInfo("家长手册", item.getParentManual(), false));

        return result;
    }

    public static List generateHighLevelItems(List<EvaluateV2ExtInfoVO> items){

        List extItems = new ArrayList();

        if (items != null)
            for (EvaluateV2ExtInfoVO i : items){
                Map<String, Object> extInfoItem = new HashMap<>();
                extInfoItem.put("dimensionTitle", i.getDimensionName());
                extInfoItem.put("month", i.getMonth());
                extInfoItem.put("sections", generateSectionMapsFromItem(i));
                extItems.add(extInfoItem);
            }

        return extItems;
    }

    public static List generateBringUpList(List<EvaluateV2ExtInfoVO> items){
        List games = new ArrayList();
        if(items == null){
            return games;
        }

        for (EvaluateV2ExtInfoVO i : items){

            String bringUpContent = i.getBringUp();
            if(StringUtils.isEmpty(bringUpContent)){
                continue;
            }
            String[] lines = bringUpContent.split("(\\n|\\r\\n)");
            for (String line : lines){
                String str = line.trim();
                if(str.length() > 0 && str.length() < 12) {

                    Map<String, Object> game = new HashMap<>();
                    game.put("dimensionName", i.getDimensionName());
                    game.put("name", line);

                    games.add(game);
                }
            }
        }
        return games;
    }

    public static Date nextEvaluateDate(Date evaluatingDate, Integer month){
        int delta = 1;
        if (month >= 24){
            delta = 2;
        }else if(month >= 36 ){
            delta = 3;
        }
        long nextTimeMills = DateUtil.plusMonths(evaluatingDate.getTime(), delta);
        Date nextDate = new Date(nextTimeMills);

        return nextDate;
    }
}
